package com.example.swd1.gudong.model;

import android.util.Log;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

//==================================================================================================================
//   Author :  Shile Wu
//   Date  :  16-8-23
//   Role :    xx
//   Reference documents : xx
//==================================================================================================================
public class Record {
    private int id;
    private float distance;
    private float avg_speed;
    private long hours;
    private float avg_pace;
    private boolean firstOfMonth;
    private int year;
    private int day;
    private int month;
    private int dayTime;
    private int currentYear;
    private int currentDay;
    private int currentMonth;

    public Record(){

    }

    public Record(int id, int year,int month,int day,int dayTime, float distance, float avg_speed, long hours, float avg_pace, boolean isFirstOfMonth) {
        this.id = id;
        this.year=year;
        this.month=month;
        this.day=day;
        this.dayTime=dayTime;
        this.distance = distance;
        this.avg_speed = avg_speed;
        this.hours = hours;
        this.avg_pace = avg_pace;
        this.firstOfMonth = isFirstOfMonth;

        Calendar calendar = Calendar.getInstance();
        currentYear = calendar.get(Calendar.YEAR);
        currentMonth = calendar.get(Calendar.MONTH) + 1;
        currentDay = calendar.get(Calendar.DAY_OF_MONTH);
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getTime() {
        String result;
        if (year == currentYear && month == currentMonth) {
            if (day == currentDay) {
                result = "今天";
            } else if (day == currentDay - 1) {
                result = "昨天";
            } else {
                result = day + "日";
            }
        } else {
            result = day + "日";
        }
        if (dayTime > 5) {
            if (dayTime < 11) {
                result += "上午";
            } else if (dayTime < 14) {
                result += "中午";
            } else if (dayTime < 18) {
                result += "下午";
            } else {
            }
        } else {
            result += "晚上";
        }
        return result;
    }

    public boolean isFirstOfMonth() {
        return firstOfMonth;
    }

    public String getDistance() {
        return distance + "";
    }

    public String getHoursStr() {
        int h, m, s;

        if (hours < 60) {
            s = (int) hours;
            m = 0;
            h = 0;
        } else {
            s = (int) (hours % 60);
            m = (int) (hours / 60);
            h = 0;
        }
        if (m > 59) {
            h = m / 60;
            m = m % 60;
        }
        return getTimeStr(h) + ":" + getTimeStr(m) + ":" + getTimeStr(s);
    }

    private String getTimeStr(int t) {
        if (t < 10) {
            return "0" + t;
        } else {
            return "" + t;
        }
    }


    public String getAvgPaceStr() {
        int m, s;
        if (avg_pace < 60) {
            s = (int) avg_pace;
            m = 0;
        } else {
            s = (int) (avg_pace % 60);
            m = (int) (avg_pace / 60);
        }
        return getTimeStr(m) + "'" + getTimeStr(s) + "\"";
    }

//    @Override
//    public String toString() {
//        return "Record{" +
//                "id=" + id +
//                ", distance=" + distance +
//                ", avg_speed=" + avg_speed +
//                ", hours=" + hours +
//                ", avg_pace=" + avg_pace +
//                ", firstOfMonth=" + firstOfMonth +
//                ", year=" + year +
//                ", day=" + day +
//                ", month=" + month +
//                ", dayTime=" + dayTime +
//                ", currentYear=" + currentYear +
//                ", currentDay=" + currentDay +
//                ", currentMonth=" + currentMonth +
//                '}';
//    }

    public String getMonthStr() {

        if (this.year == currentYear) {
            return month + "月";
        } else {
            return this.year + "年" + month + "月";
        }
    }

    public int getYearAndDay() {
        return year * 100 + month;
    }
}
